<html><head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <title>测试登录</title>
    <link rel="stylesheet" media="screen" href="dist/css/login.css">
</head>
<body>
    <!-- particles.js container -->
    <div id="particles-js" style="display: flex;align-items: center;justify-content: center">
        
    <canvas class="particles-js-canvas-el" width="1920" height="548" style="width: 100%; height: 100%;"></canvas></div>
    <div class="apTitle">登录</div>
    <div class="logcon"><input name="username" type="text" placeholder="账号">
    <input name="password" type="password" placeholder="密码">
    <button type="button" onclick="login()">登录</button>
        <button type="button" onclick="toregister()">注册</button>

    </div>


    <!-- scripts -->
    <script src="dist/js/login.js"></script>
    <script src="dist/js/loginApp.js"></script>
    <script src="plugins/jquery/jquery.min.js"></script>
    <script src="dist/js/md5.js"></script>
    <script src="dist/js/common.js"></script>
    <script src="dist/js/util.js"></script>
    <script>
        function login(){
            $.ajax({
                type: "post",
                url: "http://127.0.0.1:7001/api/ws/login",
                dataType: "json",
                data: {"username":$("input[name='username']").val(),"password":$("input[name='password']").val(),"client":"wap"},
                // xhrFields:{
                //     withCredentials:true
                // },
                //crossDomain:true,// 这两个参数要在后端需要设置withCredentials，跨域要指定域名，withCredentials:true
                beforeSend: function(xhr) {
                    var timestamp = Date.parse(new Date())/1000;
                    var jsonStr ={"appkey":"sys-server","ts":timestamp,"username":$("input[name='username']").val(),"password":$("input[name='password']").val(),"client":"wap"};
                    sign = createSign(jsonStr);
                    xhr.setRequestHeader("appkey", appkey);
                    xhr.setRequestHeader("ts", timestamp);
                    xhr.setRequestHeader("sign", sign);

                },
                error: function (e) {
                    alert("登录超时，请重新登录");

                },
                success: function (res) {
                    if(res.code==200 ){
                        alert("登录成功");
                        util.addcookie("token",res.data.token)
                        util.addcookie("userInfo",JSON.stringify(res.data.userInfo))
                        util.addcookie("userId",res.data.userInfo.id)
                        window.location.href="index.html";
                        return;
                    }else{
                        alert(res.msg);
                    }

                }
            });
        }
        function toregister() {
            location.href="register.html"
        }
    </script>
</body>
</html>